//////////////////////////////////////////
//
//	FlexInc. 2010
//	http://www.loziosecchi.it/flex_inc
//	http://twitter.com/loziosecchi
//
//////////////////////////////////////////

package flexInc.spark.effects
{
	import flash.utils.ByteArray;
	
	import mx.effects.IEffectInstance;
	
	import spark.effects.AnimateTransitionShader;
	import spark.effects.supportClasses.AnimateTransitionShaderInstance;
	
	/**
	 *  The Mosaic effect uses Pixel Bender, which is not supported
	 *  for AIR mobile applications.
	 */
	[DiscouragedForProfile("mobileDevice")]

	/**
	 *  The Mosaic effect performs a bitmap transition effect by running a
	 *  pixelization between the first and second bitmaps.
	 *  This pixelization exposes the second bitmap over the course of the 
	 *  animation. The size of the pixelization is indicated by the <code>maxPixelizationSize</code>
	 *  property.
	 * 
	 *  <p>A pixel-shader program loaded by the effect
	 *  runs the underlying bitmap effect.
	 *  If you want to use 
	 *  a different behavior, you can specify a custom pixel-shader program. 
	 *  The pixel-shader program must adhere to the constraints 
	 *  specified for the <code>shaderByteCode</code>
	 *  property of AnimateTransitionShader class, and supply three additional
	 *  parameters. 
	 *  The extra parameters required by the Mosaic shader 
	 *  are:</p>
	 *
	 *  <ul>
	 *    <li>An int <code>direction</code> parameter, 
	 *  whose value means the same as the related String property
	 *  in the Mosaic class.</li>
	 *    <li>Two floating point parameters: 
	 *  <code>imageWidth</code> and <code>imageHeight</code>. </li>
	 *  </ul>
	 *
	 *  <p>All of these parameters are set on the shader when the effect starts playing,
	 *  so the parameters need to exist and do something appropriate in
	 *  order for the effect to function correctly.</p>
	 *  
	 *  @see spark.effects.AnimateTransitionShader
	 *  @see spark.effects.AnimateTransitionShader#shaderByteCode
	 *
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion Flex 4
	 */
	public class Mosaic extends AnimateTransitionShader
	{
		[Embed(source="Mosaic.pbj", mimeType="application/octet-stream")]
		private static var MosaicShaderClass:Class;
		private static var mosaicShaderCode:ByteArray = new MosaicShaderClass();
		[Embed(source="MosaicCrossFade.pbj", mimeType="application/octet-stream")]
		private static var MosaicCrossFadeShaderClass:Class;
		private static var mosaicCrossFadeShaderCode:ByteArray = new MosaicCrossFadeShaderClass();
		[Embed(source="MosaicHex.pbj", mimeType="application/octet-stream")]
		private static var MosaicHexShaderClass:Class;
		private static var mosaicHexShaderCode:ByteArray = new MosaicHexShaderClass();
		[Embed(source="MosaicHexCrossFade.pbj", mimeType="application/octet-stream")]
		private static var MosaicHexCrossFadeShaderClass:Class;
		private static var mosaicHexCrossFadeShaderCode:ByteArray = new MosaicHexCrossFadeShaderClass();
		[Embed(source="MosaicRip.pbj", mimeType="application/octet-stream")]
		private static var MosaicRipShaderClass:Class;
		private static var mosaicRipShaderCode:ByteArray = new MosaicRipShaderClass();
		[Embed(source="MosaicRipCrossFade.pbj", mimeType="application/octet-stream")]
		private static var MosaicRipCrossFadeShaderClass:Class;
		private static var mosaicRipCrossFadeShaderCode:ByteArray = new MosaicRipCrossFadeShaderClass();
		[Embed(source="MosaicTiled.pbj", mimeType="application/octet-stream")]
		private static var MosaicTiledShaderClass:Class;
		private static var mosaicTiledShaderCode:ByteArray = new MosaicTiledShaderClass();
		[Embed(source="MosaicTiledCrossFade.pbj", mimeType="application/octet-stream")]
		private static var MosaicTiledCrossFadeShaderClass:Class;
		private static var mosaicTiledCrossFadeShaderCode:ByteArray = new MosaicTiledCrossFadeShaderClass();

		[Inspectable(enumeration="rect,rectCrossFade,hex,hexCrossFade,rip,ripCrossFade,tiled,tiledCrossFade", defaultValue="rect")]
		/**
		 *  The type of the pixelization effect: 
		 *  <code>MosaicType.RECT</code>, <code>MosaicType.RECT_CROSS_FADE</code>, 
		 *  <code>MosaicType.HEX</code>, <code>MosaicType.HEX_CROSS_FADE</code>, 
		 *  <code>MosaicType.RIP</code>, <code>MosaicType.RIP_CROSS_FADE</code>, 
		 *  <code>MosaicType.TILED</code>, <code>MosaicType.TILED_CROSS_FADE</code>, 
		 *
		 *  @default MosaicType.RECT
		 *
		 *  @see MosaicType#RECT
		 *  @see MosaicType#RECT_CROSS_FADE
		 *  @see MosaicType#HEX
		 *  @see MosaicType#HEX_CROSS_FADE
		 *  @see MosaicType#RIP
		 *  @see MosaicType#RIP_CROSS_FADE
		 *  @see MosaicType#TILED
		 *  @see MosaicType#TILED_CROSS_FADE
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion Flex 4
		 */
		public var type:String = MosaicType.RECT;
		
		/**
		 *  The maximum size of the pixelization effect. Minimum size is 2 and maximum size is 200.
		 * 
		 *  @default 50
		 *
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion Flex 4
		 */
		public var maxPixelizationSize:uint = 50;
		
		/**
		 *  Constructor. 
		 *
		 *  @param target The Object to animate with this effect.  
		 *
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion Flex 4
		 */
		public function Mosaic(target:Object=null)
		{
			super(target);

			shaderByteCode = mosaicShaderCode;
		}

		/**
		 *  @private
		 */
		override protected function initInstance(instance:IEffectInstance):void
		{
			switch (type)
			{
				case MosaicType.RECT:
					shaderByteCode = mosaicShaderCode;
					break;
				case MosaicType.RECT_CROSS_FADE:
					shaderByteCode = mosaicCrossFadeShaderCode;
					break;
				case MosaicType.HEX:
					shaderByteCode = mosaicHexShaderCode;
					break;
				case MosaicType.HEX_CROSS_FADE:
					shaderByteCode = mosaicHexCrossFadeShaderCode;
					break;
				case MosaicType.RIP:
					shaderByteCode = mosaicRipShaderCode;
					break;
				case MosaicType.RIP_CROSS_FADE:
					shaderByteCode = mosaicRipCrossFadeShaderCode;
					break;
				case MosaicType.TILED:
					shaderByteCode = mosaicTiledShaderCode;
					break;
				case MosaicType.TILED_CROSS_FADE:
					shaderByteCode = mosaicTiledCrossFadeShaderCode;
					break;
			}
			
			var properties:Object = new Object();
			properties.maxPixelizationSize = maxPixelizationSize;			
			shaderProperties = properties;

			super.initInstance(instance);
		}
	}
}